//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj首页

#import "HomeAController.h"
#import "SettingController.h"
#import "MyProfileController.h"
#import "AddMemberVC.h"
#import "MembersManageVC.h"
#import "MJCategoryRecordVC.h"
#import "MemberSelectAlertView.h"
#import "DetectionPeriodtAlertView.h"
#import "NotesGuideAlertView.h"
#import "NotesGuideVC.h"
#import "ReplaceResultsAlertView.h"
#import "MemberSelectDateAlertView.h"
#import "MJPickerViewAlertView.h"
#import "SetLoginPasswordVC.h"

@interface HomeAController ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *groupNameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

@end

@implementation HomeAController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置版本号
    [MJUIUserDeFault setObject:@"128" forKey:MJ_AppVersionCode];
    //更新版本
    [HealthHelper appUpgradeWithToast:false];
    
    [self initNotesAlertView];
    
    [WCBDHelper.shared mj_initPatientArray];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [WCBDHelper.shared initHealthData];
    
    [self requestUserData];

    [self updateUI];
  
}

- (void)mj_setUI{
    [super mj_setUI];
    

    
    [self.navigationController setNavigationBarHidden:true animated:NO];
    [self.view mj_gradientLayerWithColors:@[(id)MJUIColorFromHex(0xF2F2F7).CGColor,(id)MJUIColorFromHex(0xE1E7EA).CGColor] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
    NSLog(@"MJToken=======================%@",UserManager.shared.userToken);
    
}

#pragma mark - 方法
- (void)updateUI {
    NSString *name = UserManager.shared.userModel.userName ?: UserManager.shared.userModel.email;
    self.nameLabel.text = name ?: UserManager.shared.userModel.phoneNumber.mj_securePhoneNumber;
    self.groupNameLabel.text = UserManager.shared.userModel.groupName.length ? UserManager.shared.userModel.groupName : MJUILocalizedString(@"Group name has not been set yet");
    [self.iconImageView mj_setHeadImageWithURLString:UserManager.shared.userModel.headPic gender:UserManager.shared.userModel.gender];
}

- (void)initNotesAlertView{
  
    MJWeakSelf
    if(![[MJUIUserDeFault objectForKey:SetPassword_Open] isEqualToString:@"1"] && !UserManager.shared.userModel.havePassword) {
        
        MJUIAlertViewConfigure *configure = [[MJUIAlertViewConfigure alloc]init];
        configure.confirmButtonColor = UIColor.mj_appColor;
        configure.confirmTitle = MJUILocalizedString(@"Set Password");
        configure.cancelTitle = MJUILocalizedString(@"Not Now");
        configure.messageAlignmentCenter = true;
        configure.contentView = self.view;
        NSString *messageString = MJUILocalizedString(@"To ensure your account security, set a password now.");
        [MJUIAlertView alertViewWithTitle:MJUILocalizedString(@"Login Password Not Set") message:messageString configure:configure confirmBlock:^{
            [MJUIUserDeFault setObject:@"1" forKey:SetPassword_Open];

            SetLoginPasswordVC *vc = [[SetLoginPasswordVC alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:true];

        } cancelBlock:^{
            [MJUIUserDeFault setObject:@"1" forKey:SetPassword_Open];

        }];
    }
    if(![[MJUIUserDeFault objectForKey:NotesGuide_Open] isEqualToString:@"1"]) {
        NotesGuideAlertView *notesGuideAlertView = [[NotesGuideAlertView alloc] init];
        notesGuideAlertView.alertViewType = CenterAlertViewTypeGuide;
        notesGuideAlertView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        notesGuideAlertView.confirmBlock = ^{
            NotesGuideVC *vc = [[NotesGuideVC alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:true];
        };
        self.modalPresentationStyle = UIModalPresentationCurrentContext;
        [self.navigationController presentViewController:notesGuideAlertView animated:NO completion:nil];
    }
}

- (void)requestUserData{
    
    [MJHttpNetWorking requestWithParams:@{} requestUri:user_update requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        if(model.code == 200){
            
        }else if (model.code ==  401){
        }
    } failure:^(NSError *error) {
        
    }];
}

- (IBAction)handleSettingButtonEvent:(UIButton *)sender {
    SettingController *vc = [[SettingController alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleInfoButtonEvent:(UIButton *)sender {
    MyProfileController *vc = [[MyProfileController alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleRecordsButtonEvent:(UIButton *)sender {
    MJCategoryRecordVC *vc = [[MJCategoryRecordVC alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleMembersManagerButtonEvent:(UIButton *)sender {
    MembersManageVC *vc = [[MembersManageVC alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleAddMemberButtonEvent:(UIButton *)sender {
    AddMemberVC *vc = [[AddMemberVC alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleMultipleTestsButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeMultiple];
}

- (IBAction)handleUATestButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeGout];
}

- (IBAction)handleBGTestButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeGlu];
}

- (IBAction)handleHBTestButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeHB];
}

- (IBAction)handleBLTestButtonEvent:(UIButton *)sender {
    [self handleItemTestButtonClick:DetectionItemTypeTC];
}

- (void)handleItemTestButtonClick:(DetectionItemType)detectionItemType {
    
    MJWeakSelf
    MemberSelectAlertView *memberSelectAlertView = [[MemberSelectAlertView alloc] init];
    memberSelectAlertView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self.navigationController presentViewController:memberSelectAlertView animated:NO completion:nil];
    memberSelectAlertView.handleAddMemberBlock = ^{
        AddMemberVC *vc = [[AddMemberVC alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    memberSelectAlertView.confirmBlock = ^(PatientModel *patientModel) {
        DetectionPeriodtAlertView *detectionPeriodtAlertView = [[DetectionPeriodtAlertView alloc] init];
        detectionPeriodtAlertView.confirmBlock = ^(NSString *period) {
            [HealthHelper jumpDetectionViewController:patientModel detectionItemType:detectionItemType period:period viewController:weakSelf];
        };
        detectionPeriodtAlertView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        weakSelf.modalPresentationStyle = UIModalPresentationCurrentContext;
        [weakSelf.navigationController presentViewController:detectionPeriodtAlertView animated:NO completion:nil];
    };
}

#pragma mark - 代理

#pragma mark - 懒加载

@end
